﻿using BaoVeBienCuong.SelectMap;
using BaoVeBienCuong.Store;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BaoVeBienCuong.Playgame.army
{
    public class NangCap
    {
        private int typeLinh;// loại tướng chứa nó [--> 0 kiếm, 1 cung, 2 trượng, 3 đao <--]
        private int type; // 0 là nâng cấp tướng 1. 1 là nâng cấp tướng 2. 2 là nâng cấp sao. 3 là bán
        private int price;// giá cả cho từng nút bấm
        private Vector2 position;//vị trí
        private Vector2 size;//kích thước của từng nút
        private Texture2D image; // ảnh
        private Vector2 index;
        private float scale;
        private bool isEnable;
        private bool isSelect;
        private bool isVisible;
        private bool isLock;
        public NangCap(string Linh, int Type, Vector2 index)
        {
            isEnable = false;
            isSelect = false;
            isVisible = false;
            if (Linh.Equals("sword")) typeLinh = 0;
            else if (Linh.Equals("archer")) typeLinh = 1;
            else if (Linh.Equals("shaman")) typeLinh = 2;
            else if (Linh.Equals("knight")) typeLinh = 3;
            else typeLinh = 4;
            type = Type;
            setIndex(index);
            isLock = FileTuong.getBought((typeLinh < 2 ? typeLinh : (5 - typeLinh)) * 3 + (1 - type) + 1);
        }
        public void setIndex(Vector2 index)
        {
            this.index = index;
            size = new Vector2(GamePage.sizeImageBackGround.X / GamePage.sizeMap.Y, GamePage.sizeImageBackGround.Y / GamePage.sizeMap.X);
            position = new Vector2(index.Y * size.X, index.X * size.Y) + LechPosition() + new Vector2(0, -30);
        }
        private Vector2 LechPosition()
        {
            int temp = 90;
            if (type == 0) return new Vector2(-temp, -temp);
            else if (type == 1) return new Vector2(temp, -temp);
            else if (type == 2) return new Vector2(-temp, temp);
            else return new Vector2(temp, temp);
        }
        private void CountPrice(int sao, int level)
        {
            int tonthat = 80;
            price = 0;
            //tiền cơ bản
            #region
            if (typeLinh == 0)
            {
                #region kiếm
                if (level == 0)
                {
                    if (type == 0) price = 70;
                    else if (type == 1) price = 60;
                    else if (type == 2) price = 25;
                    else price = (50 + (sao - 1) * 25) * tonthat / 100;
                }
                else
                {
                    if (type == 2) price = 35;
                    else price = (100 + (sao - 1) * 35) * tonthat / 100;
                }
                #endregion
            }
            else if (typeLinh == 1)
            {
                #region cung
                if (level == 0)
                {
                    if (type == 0) price = 90;
                    else if (type == 1) price = 100;
                    else if (type == 2) price = 45;
                    else price = (80 + (sao - 1) * 45) * tonthat / 100;
                }
                else if (level == 1)
                {
                    if (type == 2) price = 55;
                    else price = (170 + (sao - 1) * 55) * tonthat / 100;
                }
                else
                {
                    if (type == 2) price = 60;
                    else price = (170 + (sao - 1) * 60) * tonthat / 100;
                }
                #endregion
            }
            else if (typeLinh == 2)
            {
                #region pháp sư
                if (level == 0)
                {
                    if (type == 0) price = 120;
                    else if (type == 1) price = 120;
                    else if (type == 2) price = 50;
                    else price = (100 + (sao - 1) * 50) * tonthat / 100;
                }
                else
                {
                    if (type == 2) price = 75;
                    else price = (320 + (sao - 1) * 75) * tonthat / 100;
                }
                #endregion
            }
            else if (typeLinh == 3)
            {
                #region đao
                if (level == 0)
                {
                    if (type == 0) price = 90;
                    else if (type == 1) price = 80;
                    else if (type == 2) price = 50;
                    else price = (100 + (sao - 1) * 50) * tonthat / 100;
                }
                else
                {
                    if (type == 2) price = 60;
                    else price = (285 + (sao - 1) * 60) * tonthat / 100;
                }
                #endregion
            }
            else
            {
                price = 500 * tonthat / 100;
            }
            #endregion
            SaveKyNang.CheckNangCapPrice(typeLinh, ref price);
        }// hàm này để tính toán các giá trị tiền cần để nâng cấp, đọc từ file vì phải tổng hợp nhiều dữ liệu
        public void Load(ContentManager content)
        {
            image = content.Load<Texture2D>("images/playScreen/upgradeButton");
        }
        public void Draw(SpriteBatch sprite, SpriteFont font, int sao, int level)
        {
            scale = GamePage.scale - 0.1f;
            size = new Vector2(128, 128) * scale;
            checkEnable(sao, level);

            //vẽ ảnh

            if (isVisible)
            {
                #region
                CountPrice(sao, level);
                if (type < 2) // 2 nút nâng cấp tướng 
                {
                    if (typeLinh < 2)
                    {
                        if ((!isEnable) || isSelect)
                        {
                            sprite.Draw(image, position * GamePage.scale + GamePage.DoLech, new Rectangle(typeLinh * 256 + type * 128, 128, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                        }
                        else
                        {
                            sprite.Draw(image, position * GamePage.scale + GamePage.DoLech, new Rectangle(typeLinh * 256 + type * 128, 0, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                        }
                    }
                    else
                    {
                        if ((!isEnable) || isSelect)
                        {
                            sprite.Draw(image, position * GamePage.scale + GamePage.DoLech, new Rectangle(typeLinh * 256 + (type == 0 ? 1 : 0) * 128, 128, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                        }
                        else
                        {
                            sprite.Draw(image, position * GamePage.scale + GamePage.DoLech, new Rectangle(typeLinh * 256 + (type == 0 ? 1 : 0) * 128, 0, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                        }
                    }
                }
                else // 2 nút cơ bản
                {
                    if ((!isEnable) || isSelect)
                    {
                        sprite.Draw(image, position * GamePage.scale + GamePage.DoLech, new Rectangle(128 * (11 - type), 128, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                    }
                    else
                    {
                        sprite.Draw(image, position * GamePage.scale + GamePage.DoLech, new Rectangle(128 * (11 - type), 0, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                    }
                }
                sprite.DrawString(font, price.ToString(), position * GamePage.scale + GamePage.DoLech + size - new Vector2(30, 20), Color.White);
                #endregion
            }
        }
        private void checkEnable(int sao, int level)
        {
            if (type == 3)//nút bán
            {
                isVisible = true;
                isEnable = true;
            }
            else
            {
                if (GamePage.money >= price) isEnable = true;
                else isEnable = false;

                if (typeLinh == 4)//tướng
                {
                    isVisible = false;
                }
                else
                    if (sao == 3)// full sao
                    {
                        if (type == 2)// nút nâng cấp
                        {
                            isVisible = false;
                        }
                        else
                        {
                            if (level == 0)
                            {
                                if (!isLock) isVisible = true;// cần xem xét
                                else isVisible = false;
                            }
                            else
                            {
                                isVisible = false;
                            }
                        }
                    }
                    else
                    {
                        if (type == 2)
                        {
                            isVisible = true;
                            isSelect = false;
                        }
                        else
                        {
                            isVisible = false;
                        }
                    }
            }
        }
        public bool isTouch(Vector2 touch)
        {
            isSelect = false;
            if (!isVisible || !isEnable)
            {
                return false;
            }
            else
            {
                Vector2 posi = touch - GamePage.DoLech;
                if (posi.X >= position.X * GamePage.scale && posi.X <= (position.X * GamePage.scale + size.X) && posi.Y >= position.Y * GamePage.scale && posi.Y <= (position.Y * GamePage.scale + size.Y))
                {
                    isSelect = true;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public int getType()
        {
            return type;
        }
        public int getPrice()
        {
            return price;
        }
    }
}
